<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@include file="/WEB-INF/pages/common/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
  <title>后台管理系统</title>
  <meta http-equiv="pragma" role="no-cache">
  <meta http-equiv="cache-control" role="no-cache">
  <meta http-equiv="expires" role="0">
  <meta http-equiv="keywords" role="keyword1,keyword2,keyword3">
  <meta http-equiv="description" role="This is my page">
    <%@include file="/WEB-INF/pages/common/commonCss.jsp"%>
  <!--引入树的js和css-->
  <link rel="stylesheet" href="${basePath}/resources/sg/tree/tm_tree.css"/>
  <style>
      #catogorytree{height:350px;overflow: auto;}
  </style>
<body>

<div id="catogorytree"></div>

<script type="text/javascript">
    function initCatogoryTree(callback){
        tzAjax.request({
            path:adminPath,
            model:"shop/goodcatogory",
            method:"root",
            success:function(data){
                var root  = data.root;
                var children =data.children;
                $("#catogorytree").tmTree({root:root,children:children,type:"checkbox",onclick:function($obj,data){
                    catogories = data.checkArr.opid;
                    window.catogories = catogories;
                },callback:function(opid){
                    var $this = $(this);
                    $.tzConfirm({callback:function(ok){
                        if(ok){
                            $this.closest("li").remove();
                        }
                    }});
                }});
                if(callback)callback();
            }
        });
    };

    var catogories = "";
    function saveCatogory(goodId,$dialog,opts){
        if(isEmpty(catogories)){
            loading("请选择一个分类..",4);
            return;
        }else{
            localStorage.setItem('hfl_catogories',catogories);

            if(!goodId){
                //console.log('catogories=='+JSON.stringify(catogories));
                parent.loading("选择分类成功...",4);
                $dialog.next().remove();//阴影层
                $dialog.remove();//弹窗
            }
            tzAjax.request({
                path:adminPath,
                model:"shop/good",
                method:"saveGoodAndCatogory",
                success:function(data){
                    if(data=="success"){
                        parent.loading("分类修改成功...",4);
                        $dialog.next().remove();//阴影层
                        $dialog.remove();//弹窗
                    }
                }
            },{goodId:goodId,catogories:catogories});


        }
    }

    //选中已经选择的分类
    function getChecked(opid) {
        if(opid && !localStorage.getItem("hfl_catogories")){
            $.ajax({
                type:"post",
                url:adminPath+ "/shop/good/findCatogoryByGoodId",
                data: {goodId:opid},
                success:function(data){
                    if(isNotEmpty(data)){
                        var arr = data;
                        catogories = arr;
                        localStorage.setItem('hfl_catogories',catogories);
                        for(var i=0;i<arr.length;i++){
                            $(document).find("body").find(".tm-tree-checkbox[opid="+arr[i]+"]").addClass("tm-tree-checkbox-checked");
                        }
                    }
                }
            });
        }else{
            catogories = localStorage.getItem("hfl_catogories")?localStorage.getItem("hfl_catogories").split(','):[];
            var arr = catogories;
            for(var i=0;i<arr.length;i++){
                //console.log('arr[i]=='+arr[i]);
                $(document).find("body").find(".tm-tree-checkbox[opid="+arr[i]+"]").addClass("tm-tree-checkbox-checked");
            }
        }
    }
</script>
<%@include file="/WEB-INF/pages/common/commonJS.jsp"%>
<script type="text/javascript" src="${basePath}/resources/sg/tree/tm_tree.js"></script>

</body>
</html>
